package buy;
import java.io.*;
import java.util.Date;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Buy {
	public static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	public static final String DBURL = "jdbc:sqlserver://localhost:1433;databaseName=pet";
	public static final String DBUSER = "shuo";
	public static final String DBPASS = "shuo970826";
	public static final String DBURL1 = "jdbc:sqlserver://localhost:1433;databaseName=buy";
	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;
	Connection conn1 = null;
	Statement stmt1 = null;
	ResultSet rs1 = null;

	public void shuo() {
		try {
			Class.forName(DBDRIVER);
		} catch (ClassNotFoundException e) {
		}
		try {
			conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
			stmt = conn.createStatement();
			conn1 = DriverManager.getConnection(DBURL1, DBUSER, DBPASS);
			stmt1 = conn1.createStatement();
		} catch (SQLException e) {

		}
	}

	public boolean mai(String a, String b) {
		this.shuo();
		int j = Integer.parseInt(b);
		try {
			String sqlStr = "select hao,zhong,age,shu,qian from pet";
			rs = stmt.executeQuery(sqlStr);
			while (rs.next()) {
				String hao = rs.getString(1);
				int shu = Integer.parseInt(rs.getString(4));
				if (hao.equals(a)) {
					if (shu > j) {
						sqlStr = "insert into buy values('" + rs.getString(1)
								+ "','" + rs.getString(2) + "','"
								+ rs.getString(3) + "','" + j + "','"
								+ rs.getString(5) + "','1')";
						stmt1.executeUpdate(sqlStr);
						sqlStr = "update pet set shu=shu-" + j + " where hao='"
								+ a + "'";
						stmt.executeUpdate(sqlStr);
						return true;
					} else if (shu == j) {
						sqlStr = "insert into buy values('" + rs.getString(1)
								+ "','" + rs.getString(2) + "','"
								+ rs.getString(3) + "','" + j + "','"
								+ rs.getString(5) + "','1')";
						stmt1.executeUpdate(sqlStr);
						sqlStr = "delete from pet where hao = '" + a + "'";
						stmt.executeUpdate(sqlStr);
						return true;
					} else {
						return false;
					}
				}
			}

		} catch (SQLException e) {

		}
		return false;

	}

	public void hui() {
		this.shuo();
		try {
			String sqlStr = "update buy set flag = flag+1";
			stmt1.executeUpdate(sqlStr);

		} catch (SQLException e) {

		}
	}

	public String[][] qing() {
		this.shuo();
		String[][] shuo = null;
		try {
			String sqlStr = "select count(flag) from buy where flag=1";
			rs1 = stmt1.executeQuery(sqlStr);
			rs1.next();
			shuo = new String[Integer.parseInt(rs1.getString(1))][5];
			sqlStr = "select hao,zhong,age,shu,qian from buy where flag=1";
			rs1 = stmt1.executeQuery(sqlStr);
			int i = 0, j = 0;
			while (rs1.next()) {
				
					shuo[i][j] = rs1.getString(1);
					j++;
					shuo[i][j] = rs1.getString(2);
					j++;
					shuo[i][j] = rs1.getString(3);
					j++;
					shuo[i][j] = rs1.getString(4);
					j++;
					shuo[i][j] = rs1.getString(5);
					i++;
					j=0;
				

			}
		} catch (SQLException e) {

		}
		return shuo;
	}

	public String[][] lie() {
		this.shuo();
		int i = 0, j = 0;
		String[][] shuo = null;
		try {
			String sqlStr = "select count(hao) from pet";
			rs = stmt.executeQuery(sqlStr);
			rs.next();
			shuo = new String[Integer.parseInt(rs.getString(1))][5];
			sqlStr = "select hao,zhong,age,shu,qian from pet";
			rs = stmt.executeQuery(sqlStr);
			while (rs.next()) {
				shuo[i][j] = rs.getString(1);
				j++;
				shuo[i][j] = rs.getString(2);
				j++;
				shuo[i][j] = rs.getString(3);
				j++;
				shuo[i][j] = rs.getString(4);
				j++;
				shuo[i][j] = rs.getString(5);
				i++;
				j=0;

			}

		} catch (SQLException e) {

		}
		return shuo;

	}

	public String[][] cha(String a) {
		this.shuo();
		String[][] shuo = new String[1][5];
		int i = 0, j = 0;
		try {
			String sqlStr = "select hao,zhong,age,shu,qian from pet";
			rs = stmt.executeQuery(sqlStr);
			while (rs.next()) {
				if (a.equals(rs.getString(2))) {
					shuo[i][j] = rs.getString(1);
					j++;
					shuo[i][j] = rs.getString(2);
					j++;
					shuo[i][j] = rs.getString(3);
					j++;
					shuo[i][j] = rs.getString(4);
					j++;
					shuo[i][j] = rs.getString(5);

				}

			}
		} catch (SQLException e) {

		}
		return shuo;
	}

	public boolean shan(String a) {
		this.shuo();
		try {
			String sqlStr = "delete from pet where hao = '" + a + "'";
			stmt.executeUpdate(sqlStr);
			return true;
		} catch (SQLException e) {

		}

		return false;
	}

	public boolean gai(String a, String b, String c, String d, String e) {
		int j = Integer.parseInt(c);
		int q = Integer.parseInt(d);
		int k = Integer.parseInt(e);
		this.shuo();
		try {
			String sqlStr = "update pet set zhong=" + b + ",age=" + j + ",shu="
					+ q + ",qian=" + k + " where hao='" + a + "'";
			stmt.executeUpdate(sqlStr);
			return true;
		} catch (SQLException e1) {

		}

		return false;

	}

	public boolean jia(String a, String b, String c, String d, String e) {
		int j = Integer.parseInt(c);
		int q = Integer.parseInt(d);
		int k = Integer.parseInt(e);
		this.shuo();
		try {
			String sqlStr = "insert into pet values('" + a + "','" + b + "','"
					+ j + "','" + q + "','" + k + "')";
			stmt.executeUpdate(sqlStr);
			return true;
		} catch (SQLException e1) {

		}

		return false;

	}

}
